package sk.tramtatiri.pages.users;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import sk.tramtatiri.MySession;
import sk.tramtatiri.data.dao.interfaces.FriendshipDao;
import sk.tramtatiri.data.dao.interfaces.UserDao;
import sk.tramtatiri.data.dataobjects.Friendship;
import sk.tramtatiri.data.dataobjects.User;

public class SharingPanel extends Panel {

	@SpringBean
	private FriendshipDao friendshipDao;
	@SpringBean
	private UserDao userDao;

	private List<Friendship> myFriendships = new ArrayList<Friendship>();


	/**
	 * 
	 */
	private static final long serialVersionUID = 8794266468952358616L;

	public SharingPanel(String id) {
		super(id);

		myFriendships = userDao.load(((MySession) getSession()).getUserID())
				.getFriendships();

		add(new ListView<Friendship>("userRow", myFriendships) {
			// private Label shareLabel= new Label("shareLabel", new
			// PropertyModel(this, "shareLabelText"));;
			// private String shareLabelText;
			private Friendship friendship;
			private Link share;
			private Link stopShare;

			protected void populateItem(ListItem item) {
				friendship = (Friendship) item.getModelObject();
				item.add(new Label("userNick", friendship.getFriend()
						.getNickName()));
				boolean bool = friendship.getSharedStats() == 1;
				
				share = new Link("share") {
					@Override
					public void onClick() {
						share=this;
						friendship.setSharedStats(1);
						friendshipDao.load(friendship.getId())
								.setSharedStats(1);
						this.setVisible(false);
						stopShare.setVisible(true);
					}
				};
				
				stopShare = new Link("stopShare") {
					@Override
					public void onClick() {
						stopShare=this;
						friendship.setSharedStats(0);
						friendshipDao.load(friendship.getId())
								.setSharedStats(0);
						share.setVisible(true);
						this.setVisible(false);
					}
				};
				
				if (bool) {
					share.setVisible(false);
					stopShare.setVisible(true);
				} else {
					share.setVisible(true);
					stopShare.setVisible(false);
				}
				
				item.add(share);
				item.add(stopShare);
				
			}
		});

	}

}
